<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket</title>

</head>
<body>
<h3>hello socket</h3>
<p>【我的id】：
<div><input id="userId" name="userId" type="text" value="10"></div>
<p>【发送给】：
<div><input id="toUserId" name="toUserId" type="text" value=""></div>
<p>【消息】：
<div><input id="contentText" name="contentText" type="text" value="hello websocket"></div>


<p>【开关操作】：
<button
        style="background-color: green;height: 50px;width:150px;
        line-height: 50px;color: #fff;border-radius: 10px;text-align: center;"
        onclick="openSocket()" id="button01">
    开启socket</button>
<p>【发送操作】：
<button
        style="background-color: #ffcc00;height: 50px;width:150px;
        line-height: 50px;color: #000;border-radius: 10px;text-align: center;"
        onclick="sendMessage()" id="button02">
    广播</button>
<button
        style="background-color: #ffcc00;height: 50px;width:150px;
    line-height: 50px;color: #000;border-radius: 10px;text-align: center;"
        onclick="sendMessageToOne()" id="button03">
    单发</button>

<p>【消息列表】：
<div id="newslist"></div>
</body>
<script>
    //页面加载时，随机生成用户ID
    window.onload=function(){
        document.getElementById('userId').value=Math.floor(Math.random()*100000); ;
    }

    var socket;

    function openSocket() {
        if (typeof (WebSocket) == "undefined") {
            console.log("您的浏览器不支持WebSocket");
        } else {
            console.log("您的浏览器支持WebSocket");
            //实现化WebSocket对象，指定要连接的服务器地址与端口  建立连接
            var userId = document.getElementById('userId').value;
            var socketUrl = "ws://192.168.31.150:12345/webSocket/" + userId;
            // var socketUrl = "ws://127.0.0.1.150:12345/webSocket/" + userId;
            console.log(socketUrl);
            if (socket != null) {
                socket.close();
                socket = null;
            }
            socket = new WebSocket(socketUrl);


            //打开事件
            socket.onopen = function () {
                console.log("websocket已打开");
                document.getElementById('button01').innerHTML="已开启"
            };

            //消息显示列表
            var list=[];
            //获得消息事件
            socket.onmessage = function (msg) {
                var serverMsg = msg.data;
                console.log(serverMsg);
                serverMsg=new Date().toLocaleString()+"--------"+serverMsg;
                // window.alert(serverMsg);
                //发现消息进入    开始处理前端触发逻辑
                list.push(serverMsg)
                var liststr="";
                for (var i=0;i<list.length;i++){
                    liststr+="<div>"+list[i]+"</div>"
                }
                document.getElementById('newslist').innerHTML=liststr
            };


            //关闭事件
            socket.onclose = function () {
                console.log("websocket已关闭");
                document.getElementById('button01').innerHTML="点击开启"
            };

            //发生了错误事件
            socket.onerror = function () {
                console.log("websocket发生了错误");
            }
        }
    }

    //广播
    function sendMessage() {
        if (typeof (WebSocket) == "undefined") {
            console.log("您的浏览器不支持WebSocket");
        } else {
            var sendUserId = document.getElementById('userId').value;
            var contentText = document.getElementById('contentText').value;
            var msg = '{"userId":"' + sendUserId + '","contentText":"' + contentText + '"}';
            console.log("发送"+msg);
            socket.send(msg);
        }
    }

    //单发
    function sendMessageToOne() {
        if (typeof (WebSocket) == "undefined") {
            console.log("您的浏览器不支持WebSocket");
        } else {
            var sendUserId = document.getElementById('userId').value;
            var toUserId = document.getElementById('toUserId').value;
            var contentText = document.getElementById('contentText').value;
            var msg = '{"userId":"' + sendUserId + '","toUserId":"' + toUserId + '","contentText":"' + contentText + '"}';
            console.log("发送"+msg);
            socket.send(msg);
        }
    }

</script>
</html>